<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      Array.prototype.myReduce = function (callback, Value) {
        if (this.length === 0 && Value === undefined) {
          throw new TypeError('还原没有初始值的空数组')
        }

        let sum = Value !== undefined ? Value : this[0]
        for (let i = Value !== undefined ? 0 : 1; i < this.length; i++) {
          sum = callback(sum, this[i], i, this)
        }
        // 返回累积值
        return sum
      }

      
      const res = [1, 2, 3, 4, 5, 6, 7, 8, 9].myReduce((sum, item) => {
        return sum + item
      }, 0)
      console.log(res)
    </script>
  </body>
</html>
